/*
 * Copyright (c) 2020. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.home;

import com.andremion.louvre.ResourceTable;
import com.andremion.louvre.data.Album;
import com.bumptech.glide.Glide;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.util.List;

/**
 * {@link BaseItemProvider} subclass used to bind  items from into {@link ListContainer}
 * <p>
 * We can have two types of {@link Component} items:
 */
public class GalleryAdapter extends BaseItemProvider {
    interface Callbacks {
        void onBucketClick(long bucketId, String label);

        void onMediaClick(Component imageView, Component checkView, long bucketId, int position);

        void onSelectionUpdated(int count);

        void onMaxSelectionReached();

        void onWillExceedMaxSelection();
    }

    private List<Album> albums;
    private Context mContext;
    private GalleryAdapter.Callbacks mCallbacks;

    public GalleryAdapter(Context mContext, GalleryAdapter.Callbacks mCallbacks, List<Album> albums) {
        this.mContext = mContext;
        this.albums = albums;
        this.mCallbacks = mCallbacks;
    }

    @Override
    public int getCount() {
        return albums.size();
    }

    @Override
    public Object getItem(int position) {
        return albums.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Image mImageView;
        Text mTextView;
        int screenWidth = AttrHelper.fp2px(componentContainer.getContext()
                .getResourceManager().getDeviceCapability().width, componentContainer.getContext());
        int w = screenWidth / 2;
        Uri imageLink;
        Component v = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_list_item_gallery_bucket, componentContainer, false);
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(w, w);
        v.setLayoutConfig(layoutConfig);
        mImageView = (Image) v.findComponentById(ResourceTable.Id_image);
        mImageView.setLayoutConfig(layoutConfig);
        mTextView = (Text) v.findComponentById(ResourceTable.Id_text);
        imageLink = albums.get(position).getCoverUri();
        mTextView.setText(albums.get(position).getDisplayName(componentContainer.getContext()));
        Glide.with(componentContainer.getContext())                             //配置上下文
                .load(Uri.parse(imageLink.toString()))        //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .into(mImageView);
        v.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mCallbacks.onBucketClick(0, albums.get(position).getDisplayName(componentContainer.getContext()));
            }
        });
        return v;
    }
}
